package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.ProductTag;
import com.gz.moreaseManager.modules.service.IProductTagService;
import com.gz.moreaseManager.utils.DateUtil;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.gz.moreaseManager.core.base.BaseController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 产品标签表 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-26
 */
@RestController
@RequestMapping("/tag")
public class ProductTagController extends BaseController {

    @Autowired
    private IProductTagService productTagService;

    @RequestMapping("list")
    public ModelAndView list(){
        return new ModelAndView("modules/productTag/list");
    }

    @GetMapping("listPage")
    public PageResult<ProductTag> findPageLsit(String name, Integer type,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd")Date startDate,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                             Integer pageNum, Integer pageSize){
        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<ProductTag>();
        if( UtilTool.isNotBlank( name ))
            wrapper.like("name",name);
        if( UtilTool.isNotBlank( type ))
            wrapper.eq("type",type);
        if( UtilTool.isNotBlank( startDate ) ){
            if( UtilTool.isNotBlank( endDate ) && startDate.getTime() == endDate.getTime() ){
                wrapper.between("create_time",startDate,DateUtil.addDays(startDate,1));
            }else{
                wrapper.between("create_time",startDate,
                        UtilTool.isNotBlank(endDate)?DateUtil.addDays(endDate,1):DateUtil.addDays(new Date(),1));
            }
        }
        if( UtilTool.isBlank(startDate) && UtilTool.isNotBlank( endDate )){
            wrapper.le("create_time",DateUtil.addDays(endDate,1));
        }
        //排序
        wrapper.orderBy("create_time",false);
        Page<ProductTag> data = productTagService.selectPage(paramsPage,wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());

    }

    /**
     * 新增标签
     * @param productTag
     * @return
     */
    @PostMapping("addProductTag")
    public AjaxResult<Boolean> addProductTag(ProductTag productTag ,Authentication authentication){
        if( productTag == null
                || UtilTool.isBlank( productTag.getName())
                || UtilTool.isBlank( productTag.getType()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        productTag.setCreateTime(new Date());
        //创建人
        SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
        productTag.setCreatePerson( userDetails.getUsername());
        Boolean success = productTagService.insert(productTag);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增标签信息失败!");
    }

    /**
     * 获取标签信息
     * @param id
     * @return
     */
    @GetMapping("findProductTag")
    public AjaxResult<ProductTag> findProductTag( Integer id ){
        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        ProductTag productTag = productTagService.selectById( id );
        return AjaxResult.success(productTag);

    }

    /**
     * 修改标签
     * @param productTag
     * @return
     */
    @PostMapping("editProductTag")
    public AjaxResult<Boolean> editProductTag( ProductTag productTag ){
        if( productTag == null
                || UtilTool.isBlank( productTag.getId())
                || UtilTool.isBlank( productTag.getName())
                || UtilTool.isBlank( productTag.getType()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = productTagService.insertOrUpdate(productTag);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改标签信息失败!");
    }

    /**
     * 根据ID删除标签信息
     * @param id
     * @return
     */
    @PostMapping("delete")
    public AjaxResult<Boolean> delete( Integer id){

        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = productTagService.deleteById( id );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除标签信息失败!");
    }

    /**
     * 获取系统标签列表
     * @return
     */
    @GetMapping("findList")
    public AjaxResult<List<ProductTag>> findProductTagList(){
        List<ProductTag> list = productTagService.selectList(null);
        return AjaxResult.success(list);
    }
}